package com.zut.base.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zut.base.common.entry.ResultCode;
import com.zut.base.common.entry.ResultVO;
import com.zut.base.entity.Systemlog;
import com.zut.base.entity.User;
import com.zut.base.service.ISystemlogService;
import com.zut.base.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 日志 前端控制器
 * </p>
 *
 * @author yyl
 * @since 2021-03-31
 */
@RestController
@RequestMapping("/imp/systemlog")
public class SystemlogController {
    @Autowired
    private ISystemlogService systemlogServiceImpl;

    @Autowired
    private IUserService userServiceImpl;

//    @PostMapping("/list")
//    public Object list(@RequestBody JSONObject oj) {
//        Integer current=1;
//        if(oj.getString("current")!=null && !"".equals(oj.getString("current"))) {
//            current = Integer.parseInt(oj.getString("current"));
//        }
//        Integer size = 5;
//        if(oj.getString("size")!=null && !"".equals(oj.getString("size"))) {
//            size = Integer.parseInt(oj.getString("size"));
//        }
//        String search = oj.getString("search");
////        IPage<Systemlog> systemlogIPage = new Page<>(current, size);
//        QueryWrapper<Systemlog> systemlogQueryWrapper = new QueryWrapper<>();
//        systemlogQueryWrapper.like("operation", search);
//        systemlogQueryWrapper.or();
//        systemlogQueryWrapper.like("describes", search);
//        List<Systemlog> systemlogList = null;
//        try {
//            systemlogList = systemlogServiceImpl.list(systemlogQueryWrapper);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new ResultVO<>(ResultCode.FAILED, "查找失败");
//        }
//        if(systemlogList==null || systemlogList.size()==0) {
//            systemlogList = new ArrayList<>();
//        }
//        List<Systemlog> tempList = new ArrayList<>();
//        for(Systemlog systemlog : systemlogList) {
//            QueryWrapper<User> userQueryWrapper  = new QueryWrapper<>();
//            userQueryWrapper.eq("id", systemlog.getUserid());
//            userQueryWrapper.and(i->i.like("name", search));
//            User user = userServiceImpl.getOne(userQueryWrapper);
//            if(user!=null) {
//                systemlog.setUser(user);
//                tempList.add(systemlog);
//            }
//        }
//
//        //开始手动分页
//        List<Systemlog> resultList = new ArrayList<>();
//        int total = tempList.size();
//        int start = (current-1)*size;
//        for(int i=start;i<total&&i<(start+size);i++) {
//            Systemlog systemlog = systemlogList.get(i);
//            resultList.add(systemlog);
//        }
//        Map<String, Object> map = new HashMap<>();
//        map.put("list", resultList);
//        map.put("total", total);
//        return new ResultVO<>(200, "查找成功", map);
//    }

    @PostMapping("/list")
    public Object list(@RequestBody JSONObject oj) {
        Integer current=1;
        if(oj.getString("current")!=null && !"".equals(oj.getString("current"))) {
            current = Integer.parseInt(oj.getString("current"));
        }
        Integer size = 5;
        if(oj.getString("size")!=null && !"".equals(oj.getString("size"))) {
            size = Integer.parseInt(oj.getString("size"));
        }
        String search = oj.getString("search");
        IPage<Systemlog> systemlogIPage = new Page<>(current, size);
        QueryWrapper<Systemlog> systemlogQueryWrapper = new QueryWrapper<>();
        systemlogQueryWrapper.like("operation", search);
        systemlogQueryWrapper.or();
        systemlogQueryWrapper.like("describes", search);
        IPage<Systemlog> systemlogPage = null;
        try {
            systemlogPage = systemlogServiceImpl.page(systemlogIPage, systemlogQueryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO<>(ResultCode.FAILED, "查找失败");
        }
        if(systemlogPage==null || systemlogPage.getRecords()==null) {
            return new ResultVO<>(ResultCode.FAILED, "查找失败");
        }
        for(Systemlog systemlog : systemlogPage.getRecords()) {
            User user = userServiceImpl.getById(systemlog.getUserid());
            if(user != null) {
                systemlog.setUser(user);
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("list", systemlogPage.getRecords());
        map.put("total", systemlogPage.getTotal());
        return new ResultVO<>(200, "查找成功", map);
    }
}
